/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package apache

import (
	"embed"
	"encoding/json"
	"fmt"
	"github.com/apache/incubator-answer-plugins/util"
	"io"
	"net/http"

	"github.com/apache/incubator-answer-plugins/connector-apache/i18n"
	"github.com/apache/incubator-answer/pkg/token"
	"github.com/apache/incubator-answer/plugin"
	"github.com/segmentfault/pacman/log"
)

//go:embed  info.yaml
var Info embed.FS

type Connector struct {
}

func init() {
	plugin.Register(&Connector{})
}

func (g *Connector) Info() plugin.Info {
	info := &util.Info{}
	info.GetInfo(Info)

	return plugin.Info{
		Name:        plugin.MakeTranslator(i18n.InfoName),
		SlugName:    info.SlugName,
		Description: plugin.MakeTranslator(i18n.InfoDescription),
		Author:      info.Author,
		Version:     info.Version,
		Link:        info.Link,
	}
}

func (g *Connector) ConnectorLogoSVG() string {
	return `PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIiB4bWw6c3BhY2U9InByZXNlcnZlIiB2aWV3Qm94PSIwIDAgMjM5Mi41IDQyMjYuNiI+CiAgPGRlZnM+CiAgICA8cGF0aCBpZD0iYiIgZmlsbD0iI0JFMjAyRSIgZD0iTTEzOTMuMiAxOTM0LjhjLTE1LjQgMi44LTMxLjMgNS41LTQ3LjYgOC4zLS4xIDAtLjIuMS0uMy4xIDguMi0xLjIgMTYuMy0yLjQg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zZXJTcGFjZU9uVXNlIj4KICAgIDxzdG9wIG9mZnNldD0iLjMyMzMiIHN0b3AtY29sb3I9IiM5ZTIwNjQiLz4KICAgIDxzdG9wIG9mZnNldD0iLjYzMDIiIHN0b3AtY29sb3I9IiNjOTIwMzciLz4KICAgIDxzdG9wIG9mZnNldD0iLjc1MTQiIHN0b3AtY29sb3I9IiNjZDIzMzUiLz4KICAgIDxzdG9wIG9mZnNldD0iMSIgc3RvcC1jb2xvcj0iI2U5NzgyNiIvPgogIDwvbGluZWFyR3JhZGllbnQ+CiAgPHBhdGggZmlsbD0idXJsKCNvKSIgZD0iTTE0MzMuNSAxNzM1LjZzLjEgMCAuMS0uMWMwIDAtLjEgMC0uMS4xeiIvPgogIDxwYXRoIGZpbGw9IiM2RDZFNzEiIGQ9Ik0yMjE4LjcgMzg3djExLjdoMjcuNHY3Ny40aDEyLjd2LTc3LjRoMjcuNlYzODdoLTY3Ljd6bTE1OC4zIDAtMzAuNiA2Mi4yLTMwLjgtNjIuMmgtMTUuM3Y4OS4xaDExLjd2LTcwLjdsMzAuMyA2MS4zaDguMWwzMC4zLTYxLjN2NzAuN2gxMS43VjM4N0gyMzc3eiIvPgo8L3N2Zz4=`
}

func (g *Connector) ConnectorName() plugin.Translator {
	return plugin.MakeTranslator(i18n.ConnectorName)
}

func (g *Connector) ConnectorSlugName() string {
	return "apache"
}

func (g *Connector) ConnectorSender(ctx *plugin.GinContext, receiverURL string) (redirectURL string) {
	state := token.GenerateToken()
	return fmt.Sprintf("https://oauth.apache.org/auth?state=%s&redirect_uri=%s", state, receiverURL)
}

func (g *Connector) ConnectorReceiver(ctx *plugin.GinContext, receiverURL string) (userInfo plugin.ExternalLoginUserInfo, err error) {
	code := ctx.Query("code")

	// exchange code for user info
	resp, err := http.Get("https://oauth.apache.org/token?code=" + code)
	if err != nil {
		log.Errorf("get token failed: %v", err)
		return userInfo, err
	}

	// read body
	defer resp.Body.Close()
	body, err := io.ReadAll(resp.Body)
	if err != nil {
		log.Errorf("read user info failed: %v", err)
		return userInfo, err
	}

	// parse user info
	apacheResp := OAuthResponse{}
	err = json.Unmarshal(body, &apacheResp)
	if err != nil {
		log.Errorf("parse user info failed: %v", err)
		return userInfo, err
	}

	metaInfo, _ := json.Marshal(apacheResp)
	userInfo = plugin.ExternalLoginUserInfo{
		ExternalID:  apacheResp.Uid,
		DisplayName: apacheResp.Fullname,
		Username:    apacheResp.Uid,
		Email:       apacheResp.Email,
		MetaInfo:    string(metaInfo),
	}
	return userInfo, nil
}

func (g *Connector) ConfigFields() []plugin.ConfigField {
	return []plugin.ConfigField{}
}

func (g *Connector) ConfigReceiver(config []byte) error {
	return nil
}
